//
// Created by denglibin on 2021/9/13.
//
#include "06_buffer_event_client.h"
#include "init.h"
#include "log.h"
#include <stdlib.h>
#include <event2/listener.h>
#include <event2/bufferevent.h>
#ifndef _WIN32
#include <fcntl.h>
#include <sys/types.h>
#include<unistd.h>
#include <sys/stat.h>
#include <errno.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#endif

/**
 * 写回调
 * @param be
 * @param arg
 */
static void write_cb(struct bufferevent* be, void* arg){
    zlog_info(log_get_zlog_c(), "write_cb");
    //bufferevent_disable(be, EV_WRITE); //禁用写回调
}
/**
 * 读回调
 * @param be
 * @param arg
 */
static void read_cb(struct bufferevent* be, void* arg){
    zlog_info(log_get_zlog_c(), "read_cb");
    char buf[1024] = {0};
    //读取数据
    int len = bufferevent_read(be, buf, sizeof(buf) - 1);
    if(len <= 0){
        zlog_info(log_get_zlog_c(), "read size is zero");
        return;
    }
    zlog_info(log_get_zlog_c(), "\n%s", buf);
    //发送数据
    //char data[1024] = {0};
    //int l = sprintf(data, "HTTP/1.1 200 OK\r\nContent-Length: %d\r\nContent-Type: text/html\r\n\r\n%s", len, buf);
    //bufferevent_write(be, data, l);

}
static void event_cb(struct bufferevent* be, short  what, void* arg){
    zlog_info(log_get_zlog_c(), "event_cb");
    if(what & BEV_EVENT_CONNECTED){
        zlog_info(log_get_zlog_c(), "BEV_EVENT_CONNECTED");
        //触发write
        //bufferevent_trigger(be, EV_WRITE, 0);
        //发送数据
        char* data = "hello world!";
        bufferevent_write(be, data, strlen(data));
    }

    //连接断开
    if(what & BEV_EVENT_EOF){
        zlog_info(log_get_zlog_c(), "BEV_EVENT_EOF");
        bufferevent_free(be); //清理
    }
    //错误
    if(what & BEV_EVENT_ERROR) {
        zlog_info(log_get_zlog_c(), "BEV_EVENT_ERROR");
        bufferevent_free(be); //清理
    }

    //读取超时,将不能再读取数据
    if(what & BEV_EVENT_TIMEOUT && what & BEV_EVENT_READING) {
        zlog_info(log_get_zlog_c(), "bufferevent read time out");
        bufferevent_free(be); //清理
        //bufferevent_enable(be, EV_READ); //重新设置可读
    }
}
/**
 * 客户端
 */
void demo_06(){
#ifndef _WIN32
    init();
    zlog_info(log_get_zlog_c(), "buffer event client...");
    struct event_base* base = event_base_new();
    if(!base){
        clean();
        perror("event_base_new");
        zlog_error(log_get_zlog_c(), "event_base_new fail");
        return;
    }

    struct bufferevent * bev =  bufferevent_socket_new(base, -1, BEV_OPT_CLOSE_ON_FREE);
    struct sockaddr_in client_sin;
    bzero(&client_sin, sizeof (client_sin));
    client_sin.sin_family = AF_INET;
    client_sin.sin_port = htons(5001);
    //设置地址
    evutil_inet_pton(AF_INET, "127.0.0.1", &client_sin.sin_addr.s_addr);
    //设置回调函数
    bufferevent_setcb(bev, read_cb, write_cb, event_cb, base);
    //添加监听事件
    bufferevent_enable(bev, EV_READ|EV_WRITE);
    //建立连接
    int re = bufferevent_socket_connect(bev, (struct sockaddr*)&client_sin, sizeof (client_sin));
    if(re == 0){
        zlog_info(log_get_zlog_c(), "bufferevent_socket_connect success");
    }

    //进入事件主循环
    event_base_dispatch(base);
    event_base_free(base);
    clean();
#endif
}